package com.blog.cmrpersonalblog.dto.sensitiveword.requset;

import com.blog.cmrpersonalblog.common.BasePageRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 敏感词查询请求DTO
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class SensitiveWordQueryRequest extends BasePageRequest {

    /**
     * 敏感词内容（模糊查询）
     */
    private String word;

    /**
     * 敏感词类型
     */
    private String type;

    /**
     * 严重程度
     */
    private Integer severity;

    /**
     * 过滤模式
     */
    private String filterMode;

    /**
     * 状态 0-禁用 1-启用
     */
    private Integer status;

    /**
     * 创建人ID
     */
    private Long createBy;

    /**
     * 开始时间
     */
    private String startTime;

    /**
     * 结束时间
     */
    private String endTime;

    /**
     * 标签（模糊查询）
     */
    private String tags;

    /**
     * 构造函数，设置默认排序
     */
    public SensitiveWordQueryRequest() {
        super();
        setDefaultSort("id", "asc");
    }

    /**
     * 获取安全的排序字段
     * 只允许预定义的字段进行排序，防止SQL注入
     */
    public String getSafeSortField() {
        String field = getSortField();
        if (field == null || field.trim().isEmpty()) {
            return "id";
        }

        // 允许的排序字段白名单
        switch (field.toLowerCase()) {
            case "id":
            case "word":
            case "type":
            case "severity":
            case "filter_mode":
            case "status":
            case "match_count":
            case "last_match_time":
            case "create_time":
            case "update_time":
                return field.toLowerCase();
            default:
                return "id"; // 默认按ID排序
        }
    }

    /**
     * 获取安全的排序方向
     */
    public String getSafeSortOrder() {
        String order = getSortOrder();
        if (order == null || order.trim().isEmpty()) {
            return "ASC";
        }

        return "DESC".equalsIgnoreCase(order.trim()) ? "DESC" : "ASC";
    }

    /**
     * 验证排序字段是否有效
     */
    public boolean isValidSortField(String field) {
        if (field == null || field.trim().isEmpty()) {
            return false;
        }

        String[] validFields = {"id", "word", "type", "severity", "filter_mode",
                               "status", "match_count", "last_match_time",
                               "create_time", "update_time"};

        for (String validField : validFields) {
            if (validField.equalsIgnoreCase(field.trim())) {
                return true;
            }
        }

        return false;
    }
}